from utils.baseResource import BaseResource
from utils.Response import response
from flask_restful import request, reqparse
from utils.db_seesion import fetch_to_dict_pagetion, get_count, execute
import time


class CaseSuiteList(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pid', required=True, help='pid is required')

    def get(self):
        CaseSuiteList.paser.parse_args()
        pid = request.args.get('pid')
        sname = request.args.get('sname')
        page = request.args.get('page')
        size = request.args.get('size')
        if sname:
            sql = 'select a.id,a.casesuite_name,a.casesuite_desc,a.create_time,u.nickname as create_user,a.update_time from casesuite as a inner join users as u on a.create_user=u.id where a.is_delete=0 and a.project_id =:pid and a.casesuite_name like :sname'
            result = fetch_to_dict_pagetion(sql=sql, params={'pid': pid, 'sname': '%{}%'.format(sname)}, page=page, page_size=size)
        else:
            sql = 'select a.id,a.casesuite_name,a.casesuite_desc,a.create_time,u.nickname as create_user,a.update_time from casesuite as a inner join users as u on a.create_user=u.id where a.is_delete=0 and a.project_id =:pid'
            result = fetch_to_dict_pagetion(sql=sql, params={'pid': pid}, page=page, page_size=size)
        return response(data=result)


class CaseSuiteAdd(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pid', required=True, help='pid is required')
    paser.add_argument('sname', required=True, help='sname is required')
    paser.add_argument('sdesc', required=True, help='sdesc is required')

    def post(self):
        CaseSuiteAdd.paser.parse_args()
        pid = self._datas.get('pid')
        checkPid_sql = 'select count(*) as count from project where id=:pid and is_delete=0'
        pidNum = get_count(sql=checkPid_sql, params={'pid': pid})
        if (pidNum != 1):
            return response(code=1001, msg='传入的项目id不合法')
        sname = self._datas.get('sname')
        sdesc = self._datas.get('sdesc')
        user_id = self._user_id
        create_time = time.time()
        update_time = create_time
        serch_sql = 'select count(*) as count from casesuite where project_id=:pid and casesuite_name=:sname and is_delete=0'
        count = get_count(sql=serch_sql, params={'pid': pid, 'sname': sname})
        if (count > 0):
            return response(code=1001, msg='此项目中，该用例组名称已存在')
        else:
            insert_sql = 'insert into casesuite (casesuite_name,casesuite_desc,create_time,create_user,update_time,project_id) values (:sname,:sdesc,:create_time,:user_id,:update_time,:pid)'
            execute(sql=insert_sql,
                    params={'sname': sname, 'sdesc': sdesc, 'create_time': create_time, 'user_id': user_id,
                            'update_time': update_time, 'pid': pid})
        return response()


class CaseSuiteUpdate(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pid', required=True, help='pid is required')
    paser.add_argument('sid', required=True, help='sid is required')
    paser.add_argument('sname', required=True, help='sname is required')
    paser.add_argument('sdesc', required=True, help='sdesc is required')

    def post(self):
        CaseSuiteUpdate.paser.parse_args()
        pid = self._datas.get('pid')
        sid = self._datas.get('sid')
        checkId_sql = 'select count(*) as count from project as p inner join casesuite as c on p.id=c.project_id where p.id=:pid and p.is_delete=0 and c.id=:sid and c.is_delete=0'
        idsNums = get_count(sql=checkId_sql, params={'pid': pid, 'sid': sid})
        if (idsNums != 1):
            return response(code=1001, msg='传入的项目id或测试组id不合法')
        sname = self._datas.get('sname')
        sdesc = self._datas.get('sdesc')
        update_time = time.time()
        serch_sql = 'select count(*) as count from casesuite where casesuite_name=:sname and project_id=:pid and id!=:sid and is_delete=0'
        count = get_count(sql=serch_sql, params={'sname': sname, 'pid': pid, 'sid': sid})
        if (count > 0):
            return response(code=1001, msg='此项目中已存在使用此名称的测试组，请重新输入想要修改的名称')
        else:
            update_sql = 'update casesuite set casesuite_name=:sname, casesuite_desc=:sdesc,update_time=:update_time where id=:sid'
            execute(sql=update_sql, params={'sname': sname, 'sdesc': sdesc, 'update_time': update_time, 'sid': sid})
        return response()
